package com.demo.test.juc;

import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;


/**
 * jol计算对象占用jvm内存大小
 * @author gy
 * @date 2023/7/30
 */
public class JolDemo {

    class Customer{
        int id;
        boolean flag;
    }
    public static void main(String[] args) {
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
//        System.out.println("------------------------");
//        Customer c = new Customer();
//        System.out.println(ClassLayout.parseInstance(c).toPrintable());
    }

    @Test
    public void m1(){
        long startTime = System.currentTimeMillis();
        BigDecimal a = new BigDecimal("17118829568");
        BigDecimal kb = a.divide(new BigDecimal("1024"),3,RoundingMode.HALF_UP);
        BigDecimal mb = kb.divide(new BigDecimal("1024"),3,RoundingMode.HALF_UP);
        BigDecimal gb = mb.divide(new BigDecimal("1024"),3,RoundingMode.HALF_UP);
        System.out.println("kb:"+kb+"k     mb:"+mb+"m    gb:"+gb+"g");

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    @Test
    public void m2(){
        long startTime = System.currentTimeMillis();
        HashMap<Thread, String> node = new HashMap<>();
        int threadNum = 20;
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        for (int i = 1; i <= threadNum; i++) {
            try {
                new Thread(() -> {
                    node.put(Thread.currentThread(), Thread.currentThread().getName());
                    System.out.println(Thread.currentThread().toString());
                },String.valueOf(i)).start();
            } finally {
                countDownLatch.countDown();
            }
        }
        try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); }

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


    @Test
    public void m5(){
        long startTime = System.currentTimeMillis();
        HashMap<Thread, String> node = new HashMap<>();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        long execStartTime = System.currentTimeMillis();
        for (int i = 1; i <= 100; i++) {
            executorService.execute(() -> {
                node.put(Thread.currentThread(), Thread.currentThread().getName());
                System.out.println(Thread.currentThread().toString());
            });
        }
        ReentrantLock reentrantLock = new ReentrantLock();
        executorService.shutdown();

        while(true){
            if(executorService.isTerminated()){
                //System.out.println("Finally do something ");
                long execEndTime = System.currentTimeMillis();
                System.out.println("整个任务执行耗时共计["+(execEndTime - execStartTime)+"]毫秒");
                break;
            }
        }

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }


}
